#include "muduo/protoc/codec.h"
#include "muduo/protoc/dispatcher.h"
#include "muduo/net/TcpClient.h"

#include "muduo/base/Logging.h"
#include "muduo/net/TcpClient.h"

#include "muduo/net/EventLoopThread.h"
#include "muduo/net/Callbacks.h"
#include "muduo/base/CountDownLatch.h"

#include <iostream>
#include <unistd.h>
#include <memory>
#include "request.pb.h"
class Client{
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<bit::AddRequest> AddRequestPtr;
    typedef std::shared_ptr<bit::AddResponse> AddResponsePtr;
    typedef std::shared_ptr<bit::TranslateRequest> TranslateRequestPtr;
    typedef std::shared_ptr<bit::TranslateResponse> TranslateResponsePtr;
public:
    Client(const std::string& ip, int port)
    :_latch(1)
    ,_client(_loopthread.startLoop(), muduo::net::InetAddress(ip, port), "Client")
    ,_dispatcher(std::bind(&Client::onUnknownMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    ,_codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        _dispatcher.registerMessageCallback<bit::AddResponse>(std::bind(&Client::onAdd, this, std::placeholders::_1,
                                                             std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<bit::TranslateResponse>(std::bind(&Client::onTranslate, this, std::placeholders::_1,
                                                             std::placeholders::_2, std::placeholders::_3));
        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, std::placeholders::_1, std::placeholders::_2,
                                     std::placeholders::_3));
        _client.setConnectionCallback(std::bind(&Client::onConnetion, this, std::placeholders::_1));
        
    }
    void connect(){
        _client.connect();
        _latch.wait();
    }
    void translate(const std::string& msg){
        std::cout << "translate :" << msg << std::endl;
        bit::TranslateRequest req;
        req.set_msg(msg);
        send(req);
    }
    void add(int num1, int num2){
        std::cout << "add :" << num1 << " " << num2 << std::endl;
        bit::AddRequest req;
        req.set_num1(num1);
        req.set_num2(num2);
        send(req);
    }
private:
    bool send(const google::protobuf::Message& msg){
        if(_conn->connected()){
            std::cout << "send" << std::endl;
            _codec.send(_conn, msg);
            return true;
        }
        return false;
    }
    void onConnetion(const muduo::net::TcpConnectionPtr & conn){
        if(conn->connected()){
            _conn = conn;
            _latch.countDown();
        }else{
            _conn.reset();
        }
    }
    void onUnknownMessage(const muduo::net::TcpConnectionPtr& conn, const MessagePtr& message, muduo::Timestamp){
        LOG_INFO << "onUnkownMessage: " << message->GetTypeName();
        conn->shutdown();
    }
    void onAdd(const muduo::net::TcpConnectionPtr& conn, const AddResponsePtr& message, muduo::Timestamp){
        std::cout << message->result() << std::endl;
    }

    void onTranslate(const muduo::net::TcpConnectionPtr& conn, const TranslateResponsePtr& message, muduo::Timestamp){
        std::cout << message->msg() << std::endl;
    }
private:
    muduo::CountDownLatch _latch;
    muduo::net::EventLoopThread _loopthread;
    muduo::net::TcpClient _client;
    muduo::net::TcpConnectionPtr _conn;

    ProtobufDispatcher _dispatcher;
    ProtobufCodec _codec;
};
int main()
{
    Client cli("127.0.0.1",8080);
    cli.connect();
    cli.translate("apple");
    cli.add(11,22);
    sleep(3);
    return 0;
}